 #!/usr/bin/python
 # -*- coding: utf-8 -*-

import json
import os, sys
reload(sys)
sys.setdefaultencoding('utf-8')
import traceback
import time,datetime

def format_user_time(time_str):
    try:
        t_str = time_str.split(" ") #October 3rd, 2016
        t_m = t_str[0].strip(".")
        t_d = t_str[1].strip(",").strip("st|nd|rd|th")
        t_y = t_str[2]
        month_num={"Jan":1,"Feb":2,"Mar":3,"Apr":4,"May":5,"Jun":6,"Jul":7,"Aug":8,"Sept":9,"Oct":10,"Nov":11,"Dec":12,"January":1,"February":2,"March":3,"April":4,"June":6,"July":7,"August":8,"September":9,"October":10,"November":11,"December":12}
        t_m = str(month_num[t_m])
        return (t_d,t_m,t_y)
    except:
        traceback.print_exc()

def get_reviweCntPerMonth(user_request_fin):

    #since this script need to be run within a bat script, so, we omit the header output
    #columns = ['star','summary','submitter','time_added','last_updated_since','branch','bugs_closed','diff_size', 'diff_updated','diff_updated_since','last_updated','time_added_since','my_comments','new_updates','request_count', 'repository','request_id','ship_it','target_groups','target_people','to_me']
    #Submitted邹SAMZA-32: Update Samza AM UI look and feel邹davidzchen邹May 7th, 2014, 8:25 a.m.邹July 21, 2014邹邹SAMZA-32邹+1022 -106邹May 10th, 2014, 11:03 a.m.邹May 10, 2014邹July 21st, 2014, 12:51 p.m.邹May 7, 2014邹邹邹0邹samza邹21146邹邹Samza邹邹

    #uhead="status,submitter,request_id,created_at,updated_at,group"
    #errhead="邹".join(columns)
    #print>>file("formatted_user_requests","a"), uhead
    #print>>file("err_format_user_request","a"), errhead
    user_c_request = {}
    user_u_request = {}

    for u in user_request_fin.xreadlines():
        try:
            f = u.strip().split("邹")
            status = f[0]
            if status == "Submitted":
                submitter = f[2]
                created_at = f[11]
                updated_at = f[4]
                rid = f[16]
                group = f[18]
                (created_d,created_m,created_y) = format_user_time(created_at)
                formatted_created="-".join([created_d, created_m, created_y])
                (updated_d,updated_m,updated_y) = format_user_time(updated_at)
                formatted_updated="-".join([updated_d, updated_m, updated_y])

                print>> file("formatted_user_requests","a"), ",".join([status,\
                        submitter, rid, formatted_created, formatted_updated, group])

                k_c = (submitter,"-".join([created_m,created_y]))
                k_u = (submitter,"-".join([updated_m,updated_y]))
                if k_c not in user_c_request:
                    user_c_request[k_c] = 0
                user_c_request[k_c] += 1

                if k_u not in user_u_request:
                    user_u_request[k_u] = 0
                user_u_request[k_u] += 1
        except:
            print>> file("err_user_request",'a'), u.strip()
            traceback.print_exc()

    #print>>file("user_requestCnt_created.res","w"), \
    #        "user,created_month,requestCntPerCreatedMon"

    #print>>file("user_requestCnt_updated.res","w"), \
    #        "user,updated_month,requestCntPerUpdatedMon"
    for (user,c_m) in user_c_request.keys():
        print>>file("user_requestCnt_created.res","a"), \
                ",".join([user, c_m, str(user_c_request[(user,c_m)])])
    for (user,u_m) in user_u_request.keys():
        print>>file("user_requestCnt_updated.res","a"), \
                ",".join([user, u_m, str(user_u_request[(user,u_m)])])


#python requestCntPerMonth_user.py davidzchen_requests.txt davidezchen
if __name__ == '__main__':
    user_request_fin = sys.argv[1]
    get_reviweCntPerMonth(file(user_request_fin,'r'))
